local skel = fk.CreateSkill {
  name = "lb_zero__qiansha",
}

Fk:loadTranslationTable {
  ["lb_zero__qiansha"] = "潜刹",
  [":lb_zero__qiansha"] = "当你受到伤害后，你可观看伤害来源的手牌，秘密标记其一张牌，然后其可以弃置任意张牌。出牌阶段开始时，你可清除所有牌的“潜刹”标记，若此牌在其他角色的区域内，你获得之，本回合对其使用牌无距离和次数限制且不可被抵消。",

  ["@[player]lb_zero__qiansha-turn"] = "潜刹",
  ["@lb_zero__qiansha-turn"] = "潜刹",

  ["#lb_zero__qiansha"] = "潜刹：你可以观看 %src 的手牌，秘密标记其一张牌",
  ["#lb_zero__qiansha-ask"] = "潜刹：标记 %src 一张牌",
  ["#lb_zero__qiansha-dis"] = "潜刹：你可以弃置任意张牌",

  ["#lb_zero__qiansha-draw"] = "潜刹：你可清除所有牌的“潜刹”标记，若此牌在其他角色的区域内，你获得之",

  ["$lb_zero__qiansha1"] = "从此刻开始，终幕演出。",
  ["$lb_zero__qiansha2"] = "你，才是猎物。",
  ["$lb_zero__qiansha3"] = "你已触碰到了深渊。",
}

skel:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.from and not data.from:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb_zero__qiansha:" .. data.from.id })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askToChooseCardsAndChoice(player, {
      cards = data.from:getCardIds("he"),
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      prompt = "#lb_zero__qiansha-ask:" .. data.from.id,
    })
    if #cards > 0 then
      local card = Fk:getCardById(cards[1])
      room:setCardMark(card, skel.name .. player.id, 1)
    end
    room:askToDiscard(data.from, {
      min_num = 1,
      max_num = data.from:getHandcardNum(),
      skill_name = skel.name,
      cancelable = true,
      include_equip = true,
      prompt = "#lb_zero__qiansha-dis",
    })
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb_zero__qiansha-draw" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, to in ipairs(room:getAlivePlayers()) do -- 按顺序来，避免嵌套
      for _, id in ipairs(to:getCardIds("hej")) do
        local card = Fk:getCardById(id)
        if player.dead then break end
        if card:getMark(skel.name .. player.id) == 1 then
          room:setCardMark(card, skel.name .. player.id, 0)
          room:obtainCard(player, card, false, fk.ReasonPrey, player, skel.name)
          if player ~= to then
            room:addTableMarkIfNeed(player, "@[player]lb_zero__qiansha-turn", to.id)
          end
        end
      end
    end
    for cid, cmark in pairs(room.card_marks) do
      if cmark[skel.name .. player.id] then
        room:setCardMark(Fk:getCardById(cid), skel.name .. player.id, 0)
      end
    end
  end,
})

skel:addEffect(fk.PreCardEffect, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and data.from == player and data.to and table.contains(player:getTableMark("@[player]lb_zero__qiansha-turn"), data.to.id)
  end,
  on_use = function (self, event, target, player, data)
    data:setUnoffsetable(player.room.players)
  end,
})

skel:addEffect("targetmod", {
  bypass_times = function (self, player, skill, scope, card, to)
    return player:hasSkill(skel.name) and to and card and table.contains(player:getTableMark("@[player]lb_zero__qiansha-turn"), to.id)
  end,
  bypass_distances = function (self, player, skill, card, to)
    return player:hasSkill(skel.name) and to and card and table.contains(player:getTableMark("@[player]lb_zero__qiansha-turn"), to.id)
  end,
})

skel:addLoseEffect(function(self, player, is_death)
  local room = player.room
  room:setPlayerMark(player, "@[player]lb_zero__qiansha-turn", 0)
end)

return skel
